import { MemoModel, type Memo } from "../db/model";

export class MemoService {
  async getMemos(userId: string) {
    return await MemoModel.find({ userId }).sort({ createdAt: -1 });
  }

  async createMemo(data: {
    userId: string;
    content: string;
    mood?: { level: 1 | 2 | 3 | 4 | 5 };
  }) {
    return await MemoModel.create({
      ...data,
      advice: "AI服务暂时不可用，请稍后再试。" // 默认建议
    });
  }

  async updateMemo(userId: string, memoId: string, data: {
    content?: string;
    mood?: { level: 1 | 2 | 3 | 4 | 5 };
  }) {
    const memo = await MemoModel.findOneAndUpdate(
      { _id: memoId, userId },
      { $set: data },
      { new: true }
    );

    if (!memo) {
      throw new Error("笔记不存在");
    }

    return memo;
  }

  async deleteMemo(userId: string, memoId: string) {
    const result = await MemoModel.findOneAndDelete({
      _id: memoId,
      userId,
    });

    if (!result) {
      throw new Error("笔记不存在");
    }

    return true;
  }
}
